package com;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class IOTest {
    static String path="d:/temp/io/";
    public static void main(String[] args) throws IOException {
        IOTest io=new IOTest();
        String s="我的名字是{name}";
        s=s.replace("{name}","zs");
        System.out.println(s);
        io.readFile();
        io.writefile();
        io.copyFile();
    }

    public void copyFile() throws IOException{
        FileInputStream fileInputStream = new FileInputStream(path + File.separator + "1.txt");
        FileOutputStream fout=new FileOutputStream(path + File.separator + "copy.txt");
        int i=-1;
        byte[] buff=new byte[1024];
        while ((i= fileInputStream.read(buff)) != -1){
            fout.write(buff,0, buff.length);
        }
        fileInputStream.close();
        fout.close();
        System.out.println("复制完毕");
    }
    public void writefile() throws IOException{
        FileOutputStream fout=new FileOutputStream(path+File.separator+"2.txt");
        String str="你好，世界";
        fout.write(str.getBytes(StandardCharsets.UTF_8));
        fout.close();
    }

    public void readFile() throws IOException {
        IOTest io = new IOTest();
        FileInputStream fileInputStream = new FileInputStream(path + File.separator + "1.txt");
        int i = -1;
        byte[] buff = new byte[1024];   //1k
        while ((i = fileInputStream.read(buff)) != -1) {
            System.out.println(new String(buff,"UTF-8"));
        }
        fileInputStream.close();
    }

    /**
     * 创建文件目录
     */
    public void creatFile() throws IOException {
        String path="d:/temp/io/";
        File file=new File(path);
        if (!file.exists()){
            file.mkdirs();
        }
        File myfile=new File(path,"1.txt");
        boolean newFile= myfile.createNewFile();
        System.out.println(newFile);
        //  File file=new File("d:/temp/IO/");
        //  System.out.println(file.exists());
        //  System.out.println(file.isFile());
        //  System.out.println(file.isDirectory());
        //  System.out.println(file.getPath());
        //  System.out.println(file.getAbsolutePath());
        //  System.out.println(file.getName());
        //  System.out.println(file.length());
        //  file.mkdirs();

    }
}
